// ContactAddDialog.cs
//
//  Copyright (C) 2008 Jacek Trubłajewicz
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//

using System;
using System.Drawing;
using System.Diagnostics;
using System.IO;
using Mono.Unix;
using NLog;
using HAKGERSoft;

namespace wxGadu
{
	
	public class ContactAddDialog : ContactDialog
	{
		#region Id
		protected enum Id
		{
			
		}
		#endregion

		#region controls
//		protected wx.BoxSizer topSizer;
		protected wx.Button addButton;
		protected wx.Button cancelButton;	
		#endregion
#if LOG
		private Logger logger = LogManager.GetCurrentClassLogger();
#endif

		
		public ContactAddDialog(wx.Window parent, int id, string title, Point pos, Size size, uint style):
			base(parent, id, title, pos, size, style)
		{
			addButton = new wx.Button(this, wx.Window.wxID_OK, Catalog.GetString("Add"));
			cancelButton = new wx.Button(this, wx.Window.wxID_CANCEL, Catalog.GetString("Cancel"));
			DoLayout();
			addButton.ToolTip = Catalog.GetString("Add user into your contact list");
			cancelButton.ToolTip = Catalog.GetString("Close this window without adding user to contact list");
		}
		
		private void DoLayout()
		{
			buttonSizer.Add(addButton, 0, wx.Direction.wxRIGHT|wx.Direction.wxBOTTOM, 5);
			buttonSizer.Add(cancelButton, 0, wx.Direction.wxLEFT|wx.Direction.wxRIGHT|wx.Direction.wxBOTTOM, 5);
		}
		
		public override bool TransferDataToWindow()
		{
			photoButton.BitmapLabel = mainFrame.ImageMgr[ImageManager.NOBODY];
			
			return true;
		}
		
		public override bool TransferDataFromWindow()
		{
			if (ggTC.Value == String.Empty)
			{
				wx.MessageDialog md = new wx.MessageDialog(this, Catalog.GetString("GG field cannot be empty"), Catalog.GetString("Empty field"),
			                                           wx.MessageDialog.wxOK|wx.MessageDialog.wxICON_EXCLAMATION);
				md.ShowModal();
				
				return false;
			}
			
			int val;
			if (!Int32.TryParse(ggTC.Value, out val))
			{
				wx.MessageDialog md = new wx.MessageDialog(this, Catalog.GetString("GG field must contain only numeric values"), Catalog.GetString("Illegal value"),
			                                           wx.MessageDialog.wxOK|wx.MessageDialog.wxICON_EXCLAMATION);
				md.ShowModal();
				
				return false;
			}
			
			if (mainFrame.Dispatch.ContactMgr.Contacts.ContainsKey(val))
			{
				wx.MessageDialog md = new wx.MessageDialog(this, Catalog.GetString("Entered UIN is already on the contact list"), Catalog.GetString("Illegal value"),
			                                           wx.MessageDialog.wxOK|wx.MessageDialog.wxICON_EXCLAMATION);
				md.ShowModal();
				
				return false;
			}
						
			if (nameTC.Value == String.Empty)
			{
				wx.MessageDialog md = new wx.MessageDialog(this, Catalog.GetString("Name field cannot be empty"), Catalog.GetString("Empty field"),
			                                           wx.MessageDialog.wxOK|wx.MessageDialog.wxICON_EXCLAMATION);
				md.ShowModal();
				
				return false;
			}
		
			// fill new contact object with data form controls
			ContactInfo newContactData = new ContactInfo();
			newContactData.Uin = Int32.Parse(ggTC.Value);
			newContactData.Name = nameTC.Value;
			newContactData.Firstname = firstNameTC.Value;
			newContactData.Lastname = lastNameTC.Value;
			newContactData.Location = locationTC.Value;
			newContactData.Phone = phoneTC.Value;
			
			// adds new contact to Contact Manager
			mainFrame.Dispatch.ContactMgr.AddNewContact(newContactData);
							
			// save new avatars
			string path = mainFrame.AppCfg.AvatarsDir + Path.DirectorySeparatorChar + newContactData.Uin + ".jpg";
			try
			{
				//TODO: this might throw exception as well
				photoButton.BitmapLabel.SaveFile(path, wx.BitmapType.wxBITMAP_TYPE_JPEG);
				newContactData.Photo = photoButton.BitmapLabel;
				newContactData.PhotoSmall = new wx.Bitmap(Helper.LoadAndScale(photoButton.BitmapLabel.ConvertToImage(), 80));
			}
			catch (ArgumentException)
			{		
#if LOG
				logger.Error(String.Format(Catalog.GetString("Bottom panel photo not resized correctly")));
#endif
			}

			// create new GGUser object
			GGUser newUser = new GGUser();
			newUser.GGNumber = newContactData.Uin;
			newUser.Name = newContactData.Name;
			// append it to ContactListProxy contacts list (it will be automatically added to ContactListCtrl)
			mainFrame.Dispatch.ContactProxy.AddContact(newUser);
			
			// if user is connected to GG network add
			// new contact to tracking list
			if (mainFrame.Dispatch.Gadu.IsGGLogged)
				mainFrame.Dispatch.Gadu.Users.Add(newUser);

			return true;
		}
	}
}